package com.example.pagingdemo;

import android.os.Bundle;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import androidx.paging.PagedList;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.pagingdemo.adapter.RecyclerAdapter;
import com.example.pagingdemo.bean.Book;
import com.example.pagingdemo.viewmodel.BookViewModel;

public class MainActivity extends AppCompatActivity {
    private RecyclerView mRecyclerView;
    private RecyclerAdapter adapter;
    private static final String TAG = MainActivity.class.getName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        mRecyclerView = findViewById(R.id.recyclerView);
        adapter = new RecyclerAdapter();
        BookViewModel viewModel = ViewModelProviders.of(this).get(BookViewModel.class);

        viewModel.getConvertList().observe(this, new Observer<PagedList<Book>>() {
            @Override
            public void onChanged(@Nullable PagedList<Book> Books) {
                adapter.submitList(Books);
                Log.i(TAG, "数据更新了");
            }
        });

        mRecyclerView.setAdapter(adapter);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
    }
}